package com.zeus.base.smscenter.common;

import com.alibaba.fastjson.JSON;
import com.zeus.base.privilege.common.model.BaseEntity;
import com.zeus.base.privilege.utils.QueryWhereUtil;
import com.zeus.base.smscenter.service.common.ISmsCenterBasisService;
import com.zeus.base.privilege.common.model.BasisEntity;
import com.zeus.base.privilege.common.model.Column;
import com.zeus.base.privilege.common.model.TbBasisColumn;
import com.zeus.base.privilege.model.TbUser;
import com.zeus.base.privilege.service.common.IBasisService;
import com.zeus.base.privilege.utils.ExcelUtil;
import com.zeus.base.privilege.utils.Page;
import com.zeus.base.privilege.utils.StringUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

/**
 * 基础Controller
 *
 * @author KangQi
 * @version 2016-11-11
 */
@RestController
@RequestMapping("/controller/basis")
public class BasisController extends com.zeus.base.smscenter.common.BaseController {
    @Autowired
    private IBasisService iBasisService;
    @Autowired
    private ISmsCenterBasisService iSmsCenterBasisService;

    @RequestMapping(value = "/findList", method = RequestMethod.POST)
    @ResponseBody
    public Page<Map<String, Object>> findList(Integer page, Integer rows, BasisEntity basisEntity, HttpServletRequest request) {
        basisEntity.setWhere_(QueryWhereUtil.getQueryParam(request));
        String type = request.getParameter("_type");
        if(type != null && type.equals("basis"))
            return iBasisService.findPage(basisEntity,page, rows);
        else
            return iSmsCenterBasisService.findPage(basisEntity,page, rows);
    }


    @RequestMapping(value = "/excelExport")
    @ResponseBody
    public void excelExport(String columns, String exportTitle_, BasisEntity basisEntity, HttpServletRequest request, HttpServletResponse response) throws IOException {
        basisEntity.setWhere_(QueryWhereUtil.getQueryParam(request));
        Page<Map<String, Object>> pageBean;
        String type = request.getParameter("_type");
        if(type != null && type.equals("basis"))
            pageBean = iBasisService.findPage(basisEntity,null, null);
        else
            pageBean = iSmsCenterBasisService.findPage(basisEntity,null, null);
        SXSSFWorkbook workbook = ExcelUtil.printExcel("", JSON.parseArray(columns.replaceAll("⊙", "\""), Column.class), pageBean.getRows(), pageBean.getListSum() == null || pageBean.getListSum().size() == 0 ? null : pageBean.getListSum().get(0), exportTitle_);
        exportTitle_ = new String(exportTitle_.getBytes("gbk"), "ISO-8859-1");
        response.setHeader("content-disposition", "attachment;filename=" + exportTitle_ + ".xlsx");
        workbook.write(response.getOutputStream());
    }

    @RequestMapping(value = "/findColumnConfig", method = RequestMethod.POST)
    @ResponseBody
    public String findColumnConfig(TbBasisColumn basisColumn, HttpServletRequest request) {
        if (request.getSession().getAttribute("sessionUser") == null)
            return "";
        TbUser user = (TbUser)request.getSession().getAttribute("sessionUser");
        basisColumn = iBasisService.findColumnConfig(basisColumn.getGridId(), user.getUserId());
        return basisColumn == null ? "" : basisColumn.getConfigInfo();
    }

    @RequestMapping(value = "/saveColumnConfig", method = RequestMethod.POST)
    @ResponseBody
    public String saveColumnConfig(TbBasisColumn basisColumn, HttpServletRequest request) {
        TbUser user = (TbUser)request.getSession().getAttribute("sessionUser");
        basisColumn.setUserId(user.getUserId());
        basisColumn.setCreateTime(new Date());
        iBasisService.saveColumnConfig(basisColumn);
        return "";
    }
}
